package org.example.wx.user.service.impl;

import org.example.wx.user.entity.WxUser;
import org.example.wx.user.repository.WxUserRepository;
import org.example.wx.user.service.WxUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
public class WxUserServiceImpl implements WxUserService {

    private final WxUserRepository repository;

    public WxUserServiceImpl(WxUserRepository repository) {
        this.repository = repository;
    }

    @Override
    @Transactional
    public WxUser registerIfAbsent(String openId, String nickName, String avatarUrl) {
        Optional<WxUser> optional = repository.findByOpenId(openId);
        if (optional.isPresent()) {
            WxUser exist = optional.get();
            exist.setNickName(nickName);
            exist.setAvatarUrl(avatarUrl);
            exist.setUpdateTime(LocalDateTime.now());
            return repository.save(exist);
        }
        WxUser user = new WxUser();
        user.setOpenId(openId);
        user.setNickName(nickName);
        user.setAvatarUrl(avatarUrl);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        return repository.save(user);
    }

    @Override
    public Optional<WxUser> findByOpenId(String openId) {
        return repository.findByOpenId(openId);
    }

    @Override
    @Transactional
    public WxUser registerUser(String username, String password, String nickname) {
        if (repository.existsByUsername(username)) {
            throw new RuntimeException("用户名已存在");
        }
        
        WxUser user = new WxUser();
        user.setUsername(username);
        user.setPassword(password); // 实际项目中应该加密
        user.setNickName(nickname);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        return repository.save(user);
    }

    @Override
    public Optional<WxUser> loginUser(String username, String password) {
        Optional<WxUser> userOpt = repository.findByUsername(username);
        if (userOpt.isPresent()) {
            WxUser user = userOpt.get();
            // 实际项目中应该验证加密密码
            if (password.equals(user.getPassword())) {
                return userOpt;
            }
        }
        return Optional.empty();
    }

    @Override
    public Optional<WxUser> findById(Long id) {
        return repository.findById(id);
    }

    @Override
    @Transactional
    public WxUser updateProfile(Long userId, Object updateRequest) {
        Optional<WxUser> userOpt = repository.findById(userId);
        if (!userOpt.isPresent()) {
            throw new RuntimeException("用户不存在");
        }
        
        WxUser user = userOpt.get();
        
        // 使用反射或类型转换来更新字段
        if (updateRequest instanceof java.util.Map) {
            @SuppressWarnings("unchecked")
            java.util.Map<String, Object> updates = (java.util.Map<String, Object>) updateRequest;
            
            if (updates.containsKey("nickName")) {
                user.setNickName((String) updates.get("nickName"));
            }
            if (updates.containsKey("avatarUrl")) {
                user.setAvatarUrl((String) updates.get("avatarUrl"));
            }
            if (updates.containsKey("gender")) {
                user.setGender((Integer) updates.get("gender"));
            }
            if (updates.containsKey("phoneNumber")) {
                user.setPhoneNumber((String) updates.get("phoneNumber"));
            }
            if (updates.containsKey("country")) {
                user.setCountry((String) updates.get("country"));
            }
            if (updates.containsKey("province")) {
                user.setProvince((String) updates.get("province"));
            }
            if (updates.containsKey("city")) {
                user.setCity((String) updates.get("city"));
            }
        }
        
        user.setUpdateTime(LocalDateTime.now());
        return repository.save(user);
    }
}



